package jsyf.oa.service.report.theWeather;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.facade.report.theWeather.TheWeatherFacade;
import jsyf.oa.service.wechat.service_company.HttpUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by LT on 2018/5/24.
 */
@Service
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class TheWeatherService implements TheWeatherFacade {

    @Override
    public JSONObject getTheWeather(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        JSONObject jsonObject = new JSONObject();

        if (StringUtils.isBlank(jsonParams.getString("lat")) || StringUtils.isBlank(jsonParams.getString("lng"))) {
            jsonObject.put("weather", "0");
            return jsonObject;
        }
        double lat= jsonParams.getDouble("lat");
        double lng= jsonParams.getDouble("lng");
        StringBuffer sb = new StringBuffer();
        sb.append("https://api.seniverse.com/v3/weather/daily.json?");
        sb.append("key=zjmhp4outkde5eht" + "&");
        sb.append("location=" + lat + ":" + lng+ "&");
        sb.append("language=zh-Hans" + "&");
        sb.append("unit=c" + "&");
        sb.append("start=0" + "&");
        sb.append("days=1");
        JSONObject urlJSON = HttpUtil.request(sb.toString(), "GET", null);
        if (urlJSON != null) {
            jsonParams = FastJsonConvert.convertJSONToObject(urlJSON, JSONObject.class);
            if (StringUtils.isBlank(jsonParams.getString("status"))) {
                List<JSONObject> jsonList =(List<JSONObject>) FastJsonConvert.convertJSONToArray(jsonParams.getString("results"), JSONObject.class).get(0).get("daily");
                jsonObject.put("weather",jsonList );
                return jsonObject;
            }
        }else{
            jsonObject.put("weather", "0");
        }

        return jsonObject;

    }

}

